package com.ruoyi.electronicFence.controller;

import com.ruoyi.common.config.DbInterfaceConfig;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.electronicFence.domain.vo.ReqVo;
import com.ruoyi.electronicFence.task.RegularlyModifyEnableStatusTasks;
import com.ruoyi.electronicFence.utils.HttpUtil;
import com.ruoyi.electronicFence.domain.A2ROfEFIntrusion;
import com.ruoyi.electronicFence.domain.ElectronicFence;
import com.ruoyi.electronicFence.service.ElectronicFenceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author wsl
 * @Date 2025-02-11 13:23
 * @Version 1.0
 */

@RestController
@Api(tags = "电子围栏")
@RequestMapping("/electronicFence")
@Slf4j
public class ElectronicFenceController  extends BaseController {

    @Autowired
    private ElectronicFenceService electronicFenceService;

    @Autowired
    private DbInterfaceConfig dbInterfaceConfig;

//    @Autowired
//    private ElectronicFenceMapper electronicFenceMapper;

    @Autowired
    private RegularlyModifyEnableStatusTasks regularlyModifyqyztTasks;


    @PostMapping
    @ApiOperation("电子围栏-add")
    public AjaxResult add(@RequestBody ElectronicFence electronicFence){
        return electronicFenceService.insertOne(electronicFence);
    }

    @DeleteMapping
    @ApiOperation("电子围栏-删除")
    public AjaxResult delete(@RequestParam("idList") ArrayList<Long> idList){
//        electronicFenceMapper.deleteBatchIds(idList);
        //todo sql
        StringBuilder sb = new StringBuilder("");
        sb.append("DELETE FROM J_KY_R_DZWLLB WHERE id IN (");
        idList.forEach(id -> {
           sb.append(id +",");
        });
        sb.deleteCharAt(sb.length() - 1);
        sb.append(")");
        HttpUtil.create(dbInterfaceConfig, new ReqVo(sb.toString()));
        try{
            regularlyModifyqyztTasks.initEleSet();
        }catch (Exception e) {
            log.error("更新电子围栏失败");
        }
        return AjaxResult.success();
    }

    @PutMapping
    @ApiOperation("电子围栏-update")
    public AjaxResult update(@RequestBody ElectronicFence electronicFence) {
        return electronicFenceService.updateByrwidAndRegionalId(electronicFence);
    }

    @GetMapping
    @ApiOperation("电子围栏-selectById")
    public AjaxResult selectById(Long id){
        return AjaxResult.success(electronicFenceService.selectEleById(id));
    }

    @GetMapping("listByPage")
    @ApiOperation("电子围栏-分页查询")
    public TableDataInfo list(String qymc, Integer qyzt){
        startPage();
//        List<ElectronicFence> list = electronicFenceService.listByPage(qymc, qyzt);
        StringBuilder sb = new StringBuilder("");
        sb.append("SELECT e.id, e.glid, e.qyzt, e.kssj, e.jssj, e.sjgxsj, r.qymc, r.qyjson FROM J_KY_R_DZWLLB e LEFT JOIN J_KY_S_QYXXB r ON e.glid = r.id");
        if(StringUtils.isNotBlank(qymc) || StringUtils.isNotNull(qyzt)){
            sb.append(" where 1=1");
        }
        if(StringUtils.isNotBlank(qymc)){
            sb.append(" and r.qymc like '%" + qymc + "%'");
        }
        if(StringUtils.isNotNull(qyzt)){
            sb.append("and e.qyzt = " + qyzt);
        }
        sb.append(" limit " + TableSupport.buildPageRequest().getPageSize() + " offset " + (TableSupport.buildPageRequest().getPageNum() - 1) *  TableSupport.buildPageRequest().getPageSize());
        List<Object> listBean = (List<Object>) HttpUtil.getListBean(dbInterfaceConfig, new ReqVo(sb.toString()), ElectronicFence.class);

        StringBuilder sb1 = new StringBuilder("select count(e.id) as count FROM J_KY_R_DZWLLB e LEFT JOIN J_KY_S_QYXXB r ON e.glid = r.id");
        if(StringUtils.isNotBlank(qymc) || StringUtils.isNotNull(qyzt)){
            sb1.append(" where 1=1");
        }
        if(StringUtils.isNotBlank(qymc)){
            sb1.append(" and r.qymc like '%" + qymc + "%'");
        }
        if(StringUtils.isNotNull(qyzt)){
            sb1.append("and e.qyzt = " + qyzt);
        }
        Map<String, Integer> countMap = (Map<String, Integer>) HttpUtil.getBean(dbInterfaceConfig, new ReqVo(sb1.toString()), Map.class);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(listBean);
        rspData.setTotal(countMap.get("count"));
        return rspData;
    }

    @GetMapping("selectOtherList")
    @ApiOperation("电子围栏-查询其他电子围栏")
    public AjaxResult selectOtherList(){
       return AjaxResult.success(electronicFenceService.selectOtherList());
    }


    @PostMapping("batchUpdateEnableStatus")
    @ApiOperation("电子围栏-批量修改状态")
    public AjaxResult batchUpdateqyzt(@RequestParam("idList") ArrayList<Long> idList, @RequestParam("qyzt") Integer qyzt){
        return electronicFenceService.batchUpdateqyzt(idList, qyzt);
    }

    @GetMapping("outOrIn")
    @ApiOperation("电子围栏-查询闯入闯出记录")
    public TableDataInfo selectjczt(Long id, Integer zt, String mbmc){
        startPage();
        StringBuilder sb = new StringBuilder("SELECT s.mbmc as mbmc,r.zt     " +
                "FROM J_KY_R_DZWLLB e " +
                "LEFT JOIN J_KY_S_DZWLCRJLB r ON e.id = r.rwwibh   " +
                "LEFT JOIN J_KY_S_AISADSBDWXX d ON r.dwwybh = d.id " +
                "LEFT JOIN J_KY_R_FJJCJTB s ON s.mbbm = d.mbbm where 1 = 1");
        if(StringUtils.isNotNull(id)){
            sb.append(" AND e.id = " + id);
        }
        if(StringUtils.isNotNull(zt)){
            sb.append(" and r.zt = " + zt);
        }
        if(StringUtils.isNotBlank(mbmc)){
            sb.append(" and s.mbmc like " + "'%" + mbmc +"%'");
        }
        sb.append(" limit " + TableSupport.buildPageRequest().getPageSize() + " offset " + (TableSupport.buildPageRequest().getPageNum() - 1) *  TableSupport.buildPageRequest().getPageSize());
        List<A2ROfEFIntrusion> listBean = (List<A2ROfEFIntrusion>) HttpUtil.getListBean(dbInterfaceConfig, new ReqVo(sb.toString()), A2ROfEFIntrusion.class);


        StringBuilder sb1 = new StringBuilder("SELECT count(*) as count     " +
                "FROM J_KY_R_DZWLLB e LEFT JOIN J_KY_S_DZWLCRJLB r ON e.id = r.rwwibh   " +
                "LEFT JOIN J_KY_S_AISADSBDWXX d ON r.dwwybh = d.id LEFT JOIN J_KY_R_FJJCJTB s ON s.mbbm = d.mbbm where 1 = 1");
        if(StringUtils.isNotNull(id)){
            sb1.append(" AND e.id = " + id);
        }
        if(StringUtils.isNotNull(zt)){
            sb1.append(" and r.zt = " + zt);
        }
        if(StringUtils.isNotBlank(mbmc)){
            sb1.append(" and s.mbmc like " + "'%" + mbmc +"%'");
        }
        Map<String, Integer> countMap = (Map<String, Integer>) HttpUtil.getBean(dbInterfaceConfig, new ReqVo(sb1.toString()), Map.class);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(listBean);
        rspData.setTotal(countMap.get("count"));
        return rspData;
    }









}
